package com.ruoyi.web.controller.task;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.task.domain.TaskReport;
import com.ruoyi.task.service.ITaskReportService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 任务举报Controller
 *
 * @author KevenPotter
 * @date 2024-05-02 18:57:06
 */
@RestController
@RequestMapping("/task/report")
@AllArgsConstructor
public class TaskReportController extends BaseController {

    private final ITaskReportService taskReportService;
    private final TokenService tokenService;

    /**
     * 查询任务举报列表
     */
    @PreAuthorize("@ss.hasPermi('task:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(TaskReport taskReport) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        startPage();
        List<TaskReport> list = taskReportService.getList(taskReport);
        list.forEach(item -> taskReportService.filterOutbound(item, loginUser));
        return getDataTable(list);
    }

    /**
     * 导出任务举报列表
     */
    @PreAuthorize("@ss.hasPermi('task:report:export')")
    @Log(title = "任务举报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TaskReport taskReport) {
        List<TaskReport> list = taskReportService.getList(taskReport);
        ExcelUtil<TaskReport> util = new ExcelUtil<TaskReport>(TaskReport.class);
        util.exportExcel(response, list, "任务举报数据");
    }

    /**
     * 获取任务举报详细信息
     */
    @PreAuthorize("@ss.hasPermi('task:report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        TaskReport taskReport = taskReportService.getById(id);
        taskReport = taskReportService.filterOutbound(taskReport, loginUser);
        return success(taskReport);
    }

    /**
     * 新增任务举报
     */
    @PreAuthorize("@ss.hasPermi('task:report:add')")
    @Log(title = "任务举报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TaskReport taskReport) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        taskReport = taskReportService.filterInbound(taskReport, loginUser);
        return toAjax(taskReportService.insertTaskReport(taskReport));
    }

    /**
     * 修改任务举报
     */
    @PreAuthorize("@ss.hasPermi('task:report:edit')")
    @Log(title = "任务举报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TaskReport taskReport) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        taskReport = taskReportService.filterInbound(taskReport, loginUser);
        return toAjax(taskReportService.updateTaskReport(taskReport));
    }

    /**
     * 删除任务举报
     */
    @PreAuthorize("@ss.hasPermi('task:report:remove')")
    @Log(title = "任务举报", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(taskReportService.deleteTaskReportByIds(ids));
    }
}
